const { query } = require('express')
const db = require('../db/index')

// 获取首页轮播
exports.homeImage = ((req,res)=>{
  const sql = 'select * from home_Carousel'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'获取首页轮播图成功!',
        data:result
    })
  })
})

// 美食精推
exports.recommend = ((req,res)=>{
  const sql = 'select * from DeliciousSnacks'
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'美食精推获取成功!',
        data:result
    })
  })
})

// 获取省份
exports.province = ((req,res)=>{
  const sql = 'select * from province order by province_id '
  db.query(sql,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'省份获取成功!',
        data:result
    })
  })
})

// 获取省份下的市
exports.market = ((req,res)=>{
  const user = req.query
  const sql = 'select market_id,market_name from market where province_id = ?'
  db.query(sql,user.province_id,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'市级获取成功!',
        data:result
    })
  })
})

// 获取所有省份的市
exports.AllMarket = ((req,res)=>{
  const user = req.query
  const sql = 'select market_id,market_name from market'
  db.query(sql,user.province_id,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'所有省份的市获取成功!',
        data:result
    })
  })
})

// 获取区
exports.region = ((req,res)=>{
  const user = req.query
  const sql = 'select region_id,region_name from region where market_id = (select market_id from market where market_name like ?)'
  db.query(sql,`%${user.market_name}%`,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'区域获取成功!',
        data:result
    })
  })
})

// 获取市中所有的景色数据
exports.view = ((req,res)=>{
  const {market_id} = req.query
  let sql
  /^[\d]/.test(market_id)?
    sql = 'select * from beautiful_scenery where region_name in (select region_name from region where market_id = ?)': sql = 'select * from beautiful_scenery where region_name in (select region_name from region where market_id = (select market_id from market where market_name like ?))'
  db.query(sql,`${market_id}%`,(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:'风景获取成功!',
        data:result
    })
  })
})

// 根据区域和月份获取美景数据
exports.landscape = ((req,res)=>{
  const {region_name,month_name} = req.query
  const sql = 'select a.beautiful_scenery_id,a.beautiful_scenery_name,a.introduce,a.image from  (select * from beautiful_scenery where region_name = ?) a join (select * from home_month where month_name = ?) b on a.beautiful_scenery_id = b.beautiful_scenery_id'
  db.query(sql,[region_name,month_name],(err,result)=>{
    if(err)return res.cc(err.message)
    if(result.length===0) return res.cc("获取失败!")
    res.send({
        status:0,
        message:`${region_name}${month_name}的风景获取成功!`,
        data:result
    })
  })
})